﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TCKQKL.FrontEnd.DataModel;
using TCKQKL.FrontEnd.Config;
using TCKQKL.Service.LookupClient;

namespace TCKQKL.FrontEnd.Web.Controls
{
    public partial class Authentication : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                if (isLoggedin())
                    Response.Redirect(Settings.URL_PAGE_HOME);
                LinkButton lnk = (LinkButton)Page.Master.FindControl("lnkAuthentication");
                lnk.Visible = false;
                if (IsPostBack)
                    processLogin();

            }
            catch (System.Exception ex)
            {
                throw ex;
            }

        }
        protected void processLogin()
        {
            Page.Validate();
            if (Page.IsValid)
            {
                string uid = txtAccountNo.Text.Trim();
                string pwd = txtPassword.Text.Trim();

                InvestorDataUI customer = null;
                try
                {
                    customer = userLogin(uid, pwd);
                }
                catch
                {
                    //Label lblAlert = (Label)Page.Master.FindControl("lblFullName");
                    //lblAlert.Text = Settings.ERROR_CONNECT_SERVICE;
                    //return;
                    throw;
                }
                if (customer != null)
                {
                    LSClient client = new LSClient();
                    client.PrintMessage(customer.accountNo + " has been logged in @ : " + DateTime.Now.ToString());
                    client.Close();
                    FormsAuthentication.SetAuthCookie(customer.accountNo, false);
                    if (Request["ReturnUrl"] != null)
                        //FormsAuthentication.RedirectFromLoginPage(customer.accountNo, true, Request["ReturnUrl"].Trim());
                        Response.Redirect(Request["ReturnUrl"].Trim());
                    else
                        //FormsAuthentication.RedirectFromLoginPage(customer.accountNo, true, Settings.URL_PAGE_HOME);
                        Response.Redirect(Settings.URL_PAGE_HOME);
                }
                else
                {
                    Label lblAlert = (Label)Page.Master.FindControl("lblFullName");
                    lblAlert.Text = Settings.ERROR_AUTHENTICATION;
                }
            }
        }
        protected InvestorDataUI userLogin(string user, string pass)
        {
            LSClient client = new LSClient();
            InvestorDataUI customer = client.loginProcess(user, pass);
            client.Close();
            if (customer != null)
                return customer;
            else
                return null;
        }
        protected bool isLoggedin()
        {
            string userid = HttpContext.Current.User.Identity.Name;
            if (string.IsNullOrEmpty(userid))
                return false;
            else
                return true;
        }

    }
}